<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" :placeholder="$t('Please_enter_member_ID')"/>
      <el-input v-model="listQuery.valueId" clearable class="filter-item" style="width: 200px;" :placeholder="$t('Please_enter_goods_ID')"/>
      <el-button class="filter-item" type="info" icon="el-icon-search" @click="handleFilter">{{$t('Search')}}</el-button>
<!--      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">{{$t('Find')}}</el-button>-->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" border fit highlight-current-row>
      <el-table-column type="index" :index="getIndex" width="50"></el-table-column>

      <el-table-column align="center" width="100px" :label="$t('收藏ID')" prop="id" sortable/>

      <el-table-column align="center" min-width="100px" :label="$t('Member_ID')" prop="userId"/>

      <el-table-column align="center" min-width="100px" :label="$t('User_name')" prop="userName">
        <template slot-scope="scope">
          <el-tag >{{ maskStr(scope.row.userName) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="100px" :label="$t('Merchandise_ID')" prop="valueId"/>

      <el-table-column align="center" min-width="100px" :label="$t('Merchandise_name')" prop="valueName"/>

      <!-- <el-table-column align="center" min-width="100px" :label="$t('Time_added')" prop="addTime"/> -->

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { listCollect } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Collect',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userId: undefined,
        valueId: undefined,
        sort: 'id',
        order: 'desc'
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listCollect(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        userId: '',
        valueId: '',
        addTime: undefined
      }
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = [this.$t('用户ID'), this.$t('商品ID'), this.$t('添加时间')]
        const filterVal = ['userId', 'valueId', 'addTime']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, this.$t('用户收藏信息'))
        this.downloadLoading = false
      })
    },
    maskStr(str) {
      if (str) {
        return str.substr(0, 3) + '****' + str.substr(7, str.length)
      } else {
        return undefined
      }
    },
    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    }
  }
}
</script>
